package com.topisv.tms.service.edi.ocr;

import java.util.Date;
import java.util.List;

import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.topisv.tms.entity.EdiGoodsSku;
import com.topisv.tms.entity.EdiOrder;
import com.topisv.tms.entity.EdiOrderItem;
import com.topisv.tms.repository.EdiGoodsSkuDao;
import com.topisv.tms.repository.EdiOrderDao;
import com.topisv.tms.repository.EdiOrderItemDao;
import com.topisv.tms.utils.JsonUtil;
import com.topisv.tms.webservice.soap.response.vo.ReturnMessage;

@Component
@Transactional(readOnly = true)
public class AddOCRSkuImpl {
	@Autowired
	private EdiOrderItemDao ediOrderItemDao;
	@Autowired
	private EdiGoodsSkuDao ediGoodsSkuDao;
	@Autowired
	private EdiOrderDao ediOrderDao;

	/**
	 * 增加商品信息
	 * 
	 * @param key
	 * @param secret
	 * @param message
	 * @return
	 * @throws Exception
	 */
	@Transactional(rollbackFor=Exception.class,readOnly = false)
	public ReturnMessage addOCRSku(String key, String secret, String message)
			throws Exception {
		List<EdiOrderItem> objList = null;
		ReturnMessage returnMessage = null;
		try {
			returnMessage = new ReturnMessage();

			if (message.startsWith("{")) {
				JSONObject jsonMessage = JSONObject.fromObject(message);
				EdiOrderItem ediOrderItem = (EdiOrderItem) JSONObject.toBean(jsonMessage, EdiOrderItem.class);
				save(ediOrderItem, key);
			} else {
				objList = JsonUtil.getList(message, EdiOrderItem.class);
				save((List<EdiOrderItem>) objList, key);
			}

			returnMessage.setStatus("1");
			returnMessage.setReson("接收成功");

		} catch (Exception e) {
			throw e;
		}
		return returnMessage;
	}

	/**
	 * 增加多件商品明细，先删除再增加
	 * 
	 * @param ediOrderItems
	 * @throws Exception
	 */
	@Transactional(rollbackFor=Exception.class,readOnly = false)
	public void save(List<EdiOrderItem> ediOrderItems, String customerCode)
			throws Exception {
		try {
			for (EdiOrderItem ediOrderItem : ediOrderItems) {
				save(ediOrderItem,customerCode);
			}
	     } catch (Exception e) {
			throw e;
		}
	}

	/**
	 * 更新或者增加商品信息
	 * 
	 * @param ediOrderItem
	 * @param customerCode
	 * @throws Exception
	 */
	@Transactional(rollbackFor=Exception.class,readOnly = false)
	private void saveGoods(EdiOrderItem ediOrderItem, String customerCode)
			throws Exception {
		try {
			EdiGoodsSku es = ediGoodsSkuDao.findByCustomerCodeAndSku(customerCode, ediOrderItem.getSku());
			if (es == null) {
				es = new EdiGoodsSku();
				es.setSku(ediOrderItem.getSku());
				es.setCustomerCode(customerCode);
				es.setStoreCode(ediOrderItem.getStoreCode());
				es.setStoreId(ediOrderItem.getStoreId());
				es.setSkuName(ediOrderItem.getNam());
			} else {
				if (es.getStoreCode() == null) {
					es.setStoreCode(ediOrderItem.getStoreCode());
				} else if (ediOrderItem.getStoreCode() != null&& !es.getStoreCode().equals(ediOrderItem.getStoreCode())) {
					es.setStoreCode(ediOrderItem.getStoreCode());
				}
			}

			ediGoodsSkuDao.save(es);
		} catch (Exception e) {
			throw e;
		}

	}

	@Transactional(rollbackFor=Exception.class,readOnly = false)
	private void delete(EdiOrderItem ediOrderItem, String customerCode)throws Exception {
		try {
			//saveGoods(ediOrderItem, customerCode);
			ediOrderItemDao.deleteOrdeno(customerCode,ediOrderItem.getOrdeno());
			ediOrderItem.setCustomerCode(customerCode);// 设置客户编码
		} catch (Exception e) {
			throw e;
		}
	}

	/**
	 * 商品信息
	 * 
	 * @param ediOrderItem
	 * @param username
	 *            客户编码
	 * @throws Exception
	 */
	@Transactional(rollbackFor=Exception.class,readOnly = false)
	public void save(EdiOrderItem ediOrderItem, String customerCode)
			throws Exception {
		if (ediOrderItem.getQty() == null) {
			throw new Exception("商品数量不能为null");
		}
		if (ediOrderItem.getQty() < 0) {
			throw new Exception("商品数量不能为:"+ediOrderItem.getQty());
		}
		if(ediOrderItem.getSku()==null||"".equals(ediOrderItem.getSku().trim())){
			throw new Exception("商品sku编码不能为:"+ediOrderItem.getSku());
		}
		try {
			ediOrderItem.setCrtime(new Date());
			delete(ediOrderItem, customerCode);
			List<EdiOrder> orders = ediOrderDao.findByCustomerCodeAndCustomerOrdeno(customerCode, ediOrderItem.getOrdeno());
			EdiOrder ediOrder = orders.isEmpty()? null : orders.get(0);
			if(ediOrder!=null){
				ediOrderItem.setOrdeno(ediOrder.getOrdeno());
			}
			ediOrderItemDao.save(ediOrderItem);
		} catch (Exception e) {
			throw e;
		}
	}
}
